package cn.lwx.javaweb.web.action;

import cn.lwx.javaweb.utils.StudentUtils;
import cn.lwx.javaweb.bean.StudentInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * 学生信息模板类
 *
 * @author lwx
 * @date 2022-05-25 代码重构
 */
@WebServlet({"/stu/list", "/stu/add", "/stu/edit", "/stu/detail", "/stu/del", "/stu/modify","/stu/query"})
public class StudentInfoServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    // 模板方法
    // 重写service方法
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 获取servlet path
        String servletPath = request.getServletPath();
        if ("/stu/list".equals(servletPath)) {
            doList(request, response);
        } else if ("/stu/add".equals(servletPath)) {
            doAdd(request, response);
        } else if ("/stu/detail".equals(servletPath)) {
            doDetail(request, response);
        } else if ("/stu/del".equals(servletPath)) {
            doDel(request, response);
        } else if ("/stu/modify".equals(servletPath)) {
            doModify(request, response);
        } else if ("/stu/query".equals(servletPath)) {
            doQuery(request, response);
        }
    }

    /**
     * 根据姓名查询
     */
    private void doQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sName = request.getParameter("sName");
        List<StudentInfo> studentInfoList = StudentUtils.queryByName(sName);
        HttpSession session = request.getSession();
        session.setAttribute("studentInfoList", studentInfoList);
        request.getRequestDispatcher("/table.jsp").forward(request, response);
    }

    /**
     * 查询全部学生信息
     */
    private void doList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<StudentInfo> studentInfoList = StudentUtils.queryAll();
        HttpSession session = request.getSession();
        session.setAttribute("studentInfoList", studentInfoList);
        request.getRequestDispatcher("/table.jsp").forward(request, response);
    }

    /**
     * 添加学生信息
     */
    private void doAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        String sName = request.getParameter("sName");
        String sClass = request.getParameter("sClass");
        String sNo = request.getParameter("sNo");
        String sCollege = request.getParameter("sCollege");
        String sSex = request.getParameter("sSex");

        StudentInfo user = new StudentInfo();
        user.setsName(sName);
        user.setsClass(sClass);
        user.setsNo(sNo);
        user.setsCollege(sCollege);
        user.setsSex(sSex);

        if (StudentUtils.insert(user) == 1) {
            response.sendRedirect(request.getContextPath() + "/stu/list");
        } else {
            response.sendRedirect(request.getContextPath() + "/failuserinfo.jsp");
        }

    }

    /**
     * 根据id获取学生信息详情
     */
    private void doDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String id = request.getParameter("id");

        StudentInfo studentInfo = StudentUtils.queryById(id);

        request.setAttribute("studentInfo", studentInfo);

        request.getRequestDispatcher("/" + request.getParameter("type") + ".jsp").forward(request, response);
    }

    /**
     * 删除学生信息
     */
    private void doDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");

        int n = StudentUtils.remove(id);
        if (n == 1) {
            System.out.println("删除成功");
            response.sendRedirect(request.getContextPath() + "/stu/list");
        } else {
            System.out.println("删除失败");
        }
    }

    /**
     * 修改学生信息
     */
    private void doModify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        String id = request.getParameter("id");
        String sName = request.getParameter("sName");
        String sClass = request.getParameter("sClass");
        String sNo = request.getParameter("sNo");
        String sCollege = request.getParameter("sCollege");
        String sSex = request.getParameter("sSex");

        int n = StudentUtils.update(id, sName, sClass, sNo, sCollege, sSex);
        if (n > 0) {
            System.out.println("修改成功");
            request.getRequestDispatcher("/stu/list").forward(request, response);
        } else {
            System.out.println("修改失败");
        }
    }
}
